#<#
# TroubleshooterScript - This script checks for the presence of a root cause
# Key Cmdlets:
# -- update-diagrootcause flags the status of a root cause and can be used to pass parameters
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user
##>

#Load functions 
. ./FunctionsLib.ps1

#Define an array to collect RC instance IDs (settings Group IDs) for which the resolver already ran
$Global:RS_alreadyRun =@()

#Define a hash table to collect RC instance IDs (settings Group IDs) and their status(verified or not) 
#for which the verifier already ran
$Global:Verified =@{}

Import-LocalizedData -BindingVariable Strings_TS -FileName LocalizationData
Write-DiagProgress -activity $Strings_TS.ID_PROG_DETECT
sleep -Milliseconds 500

#Given an ID of a settings group and array of Settings Groups
#returns the display name of a settings Group that matches the given ID
function GetGroupName
{  
	param ($ID, $choices)	
    $name = $null
	foreach ($choice in $choices)
	{  
	   if ($ID  -ieq ($choice["Value"]))
	   {
	     $name = $choice["Name"]		
		 break
	   }	   
	}   
	return $name
}    
#Given an ID of a settings group and array of Settings Groups
#returns the description of a settings Group that matches the given ID
function GetGroupDescription
{  
	param ($ID, $choices)	
    $desc = $null
	foreach ($choice in $choices)
	{  
	   if ($ID  -ieq ($choice["Value"]))
	   {
	     $desc = $choice["Description"]
		 break
	   }	   
	}
	return $desc
}


#Determine whether the XML and XSD files exist in the current directory
if ((Test-Path "Settings.xml") -and (Test-Path Settings.xsd)) 
{  
	# Both files exist.Determine the validity of the settings xml against the given schema
	# get the error message if it is not valid
	$result = checkValidityOfXML "Settings.xml" "Settings.xsd"
		
	if($result -eq $null)
	{ # No XML exception is thrown. Continue loading and processing the xml		
		
		#Load XML from disk
		[xml]$xml = Get-Content "Settings.xml" -Force
	   
		#Get the settings Groups from the XML  
		$groups = $xml.SelectNodes("/SoftwareReset/*")	
	     
		if ($groups -ne $null)
		 {		     
			$choices = @()
			Foreach ($group in $groups) 
			{
			 $GroupID = $group.Attributes.GetNamedItem("id").value		 
			 $GroupName = $Group.SelectSingleNode("DisplayInformation/Name").innertext		 
			 $GroupDescription = $Group.SelectSingleNode("DisplayInformation/Description").innertext		  
			 $choices += @{"Name"= $GroupName; "Value"= $GroupID; "Description"= $GroupDescription; "ExtensionPoint"=""}		 
			}  	

		#Get selection type (Single/Multiple) of the settings groups from the settings.xml
	    $selectionType = ($xml.SelectSingleNode("/SoftwareReset")).Attributes.GetNamedItem("selectionType").value
		
		#Check if there is more that one settings group 
		if ($choices.count -gt 1)
		{	
			If ($selectionType)
			{
				#Determine whether the selection type is Single or Multiple 
			    if($selectionType -ieq "Single")
				{
				 #Prompt the user to select ONLY ONE settings group
				 $IDs = get-diaginput -id "INT_SingleSelectSettingsGroup" -Choice $choices 
				}
				else
				{		
			     #Prompt the user to select ONE or MORE settings groups 
			     $IDs = get-diaginput -id "INT_MultipleSelectSettingsGroup" -Choice $choices
			    }
			}
			else
			{
			#Prompt the user to select ONE or MORE settings groups 
			$IDs = get-diaginput -id "INT_MultipleSelectSettingsGroup" -Choice $choices 
			}
		}
		else
		{ 
		 #There is only one settings group. 
		 $IDs = $GroupID	   
		}

			#Based on the selected settings group(s), notify Windows Troubleshooting Platform of the status of this root cause
			If($IDs -ne $null)
			{
			  foreach ($ID in $IDs)
			  {
				#Get the display name and description of the selected settings group
				$name = GetGroupName $ID $choices	
				$description = GetGroupDescription $ID $choices
				Update-DiagRootCause -Id "RC_AppSettings" -Detected $true -InstanceId $ID -Parameter @{"id"=$ID; "name"=$name; "description"=$description } 		  
			  }
			}
	  }  
	} # end if (checkValidityOfXML)
	else
	{    
		# Xml is not valid. Add the error message to the troubleshooting report
		$result | convertto-xml | update-diagreport  -id "RC_AppSettings" -name "Xml Validation Error" -Verbosity Informational
	}
} 
